/**
 * Copyright &copy; 2017-2020 <a href="http://www.fosung.com/">FosungFrame</a> All rights reserved.
 */
package com.fosung.frame.modules.approve.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.fosung.frame.common.config.Global;
import com.fosung.frame.common.persistence.Page;
import com.fosung.frame.common.utils.StringUtils;
import com.fosung.frame.common.web.BaseController;
import com.fosung.frame.modules.approve.entity.HomeApprove;
import com.fosung.frame.modules.approve.service.HomeApproveService;
import com.fosung.frame.modules.approve.service.PartyScoreService;
import com.fosung.frame.modules.checkinfos.service.CheckinfosService;
import com.fosung.frame.modules.score.entity.OrganizePersonDetail;
import com.fosung.frame.modules.score.entity.PartyScoreDetail;
import com.fosung.frame.modules.score.entity.PersonScoreDetail;
import com.fosung.frame.modules.score.service.OrganizePersonDetailService;
import com.fosung.frame.modules.sys.utils.UserUtils;

/**
 * 活动积分申请管理Controller
 * 
 * @author ygl
 * @version 2017-08-30
 */
@Controller
@RequestMapping(value = "${adminPath}/approve/activityApprove")
public class ActivityApproveController extends BaseController {

	@Autowired
	private HomeApproveService homeApproveService;

	@Autowired
	private OrganizePersonDetailService organizePersonDetailService;

	@Autowired
	private CheckinfosService checkinfosService;

	@Autowired
	private PartyScoreService partyScoreService;

	@ModelAttribute
	public HomeApprove get(@RequestParam(required = false) String id) {
		HomeApprove entity = null;
		if (StringUtils.isNotBlank(id)) {
			entity = homeApproveService.get(id);
		}
		if (entity == null) {
			entity = new HomeApprove();
		}
		return entity;
	}

	@RequiresPermissions("approve:activityApprove:view")
	@RequestMapping(value = { "list", "" })
	public String list(HomeApprove homeApprove, HttpServletRequest request, HttpServletResponse response, Model model) {
		homeApprove.setOrganizationid(UserUtils.getUser().getOffice().getId());
		Page<HomeApprove> page = homeApproveService.findActivityApprovePage(new Page<HomeApprove>(request, response), homeApprove);
		model.addAttribute("page", page);
		return "modules/approve/activityApproveList";
	}

	/**
	 * 组织生活积分管理功能 审核详情页
	 * 
	 * @param organizeLife
	 * @param model
	 * @return
	 */
	@RequiresPermissions("approve:activityApprove:view")
	@RequestMapping(value = "form")
	public String form(HomeApprove homeApprove, Model model) {
		String username = UserUtils.getUser().getName();
		model.addAttribute("username", username);
			
		// 循环获取审批意见，然后赋值
		homeApprove = get(homeApprove.getId());
		homeApprove.setCheckinfos(checkinfosService.findCheckinfosByOne(homeApprove));
		model.addAttribute("homeApprove", homeApprove);
		
		//获取党组织积分明细
		PartyScoreDetail partyScoreDetail = new PartyScoreDetail();
		partyScoreDetail.setOrganizeLifeId(homeApprove.getId());
		partyScoreDetail.setOrganizeId(homeApprove.getOrganizationid());
		partyScoreDetail.setYear(homeApprove.getColumn24());
		List<PartyScoreDetail> partyScoreDetailList = partyScoreService.findPartyScoreDetailList(partyScoreDetail);

		// 获取党员的积分项
		PersonScoreDetail personScoreDetail = new PersonScoreDetail();
		personScoreDetail.setOrganizeLifeId(homeApprove.getId());
		List<PersonScoreDetail> personScoreDetailList = partyScoreService.findPersonScoreDetailList(personScoreDetail);

		// 获取签到党员信息
		OrganizePersonDetail organizePersonDetail = new OrganizePersonDetail();
		organizePersonDetail.setOrganizeLifeId(homeApprove.getId());
		List<OrganizePersonDetail> organizePersonDetailList = partyScoreService.findOrganizePersonDetailList(organizePersonDetail);
	
		model.addAttribute("homeApprove", homeApprove);
		model.addAttribute("partyScoreDetailList", partyScoreDetailList);
		model.addAttribute("personScoreDetailList", personScoreDetailList);
		model.addAttribute("organizePersonDetailList", organizePersonDetailList);
		return "modules/approve/activityApproveForm";
	}

	/**
	 * 校验党组织积分是否超过年度上限
	 * @param partyScoreDetail
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("approve:activityApprove:view")
	@RequestMapping(value = "checkPartyTotalUppper")
	@ResponseBody
	public String checkPartyTotalUppper(PartyScoreDetail partyScoreDetail, RedirectAttributes redirectAttributes) {
		String isUpperStatus =	partyScoreService.isUpperPartyTotalScore(partyScoreDetail);
		String msg = "{\"result\":\""+isUpperStatus+"\"}";
		return msg;
	}
	
	/**
	 * 校验党员积分是否超过年度上限
	 * @param partyScoreDetail
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("approve:activityApprove:view")
	@RequestMapping(value = "checkPersonTotalUppper")
	@ResponseBody
	public String checkPersonTotalUppper(OrganizePersonDetail organizePersonDetail, RedirectAttributes redirectAttributes) {
		String isUpperStatus = organizePersonDetailService.isUpperPersonTotalScore(organizePersonDetail);
		String msg = "{\"result\":\""+isUpperStatus+"\"}";
		return msg;
	}
	
	
	/**
	 * 同意操作
	 * @param organizeLife
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("approve:activityApprove:view")
	@RequestMapping(value = "agreeOrganizeLifeScore")
	@ResponseBody
	public String agreeOrganizeLifeScore(@RequestBody HomeApprove homeApprove, RedirectAttributes redirectAttributes) {
		String msg = partyScoreService.agreeOrganizeLifeScore(homeApprove);
		return msg;
	}
	
	/**
	 * 退回、拒绝党组织积分申请操作
	 * @param partyScoreDetail
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("approve:activityApprove:view")
	@RequestMapping(value = "rejectOrganizeLifeScore")
	@ResponseBody
	public String rejectOrganizeLifeScore(@RequestBody HomeApprove homeApprove, RedirectAttributes redirectAttributes) {
		String msg = partyScoreService.rejectOrganizeLifeScore(homeApprove);
		return msg;
	}
	
	/**
	 * 批量退回、拒绝组织生活申请操作
	 * @param homeApprove
	 * @param redirectAttributes
	 * @return
	 */
	@RequiresPermissions("approve:activityApprove:view")
	@RequestMapping(value = "batchUpdateOrganizeLifeStatus")
	public String batchUpdateOrganizeLifeStatus(HomeApprove homeApprove, RedirectAttributes redirectAttributes) {
		String msg = partyScoreService.batchUpdateOrganizeLifeStatus(homeApprove);
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/approve/activityApprove/?repage";
	}
	
	/**
	 * 批量同意时，校验是否超过上限
	 * @param partyScoreDetail
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "checkScoreTotal")
	@ResponseBody
	public String checkScoreTotal(HomeApprove homeApprove, RedirectAttributes redirectAttributes) {
		return partyScoreService.checkScoreTotal(homeApprove);
	}
	
	/**
	 * 批量同意积分申请操作
	 * @param homeApprove
	 * @param redirectAttributes
	 * @return
	 */
	@RequestMapping(value = "batchAgreeOrganizeLife")
	public String batchAgreeOrganizeLife(HomeApprove homeApprove, RedirectAttributes redirectAttributes) {
		String msg = partyScoreService.batchAgreeOrganizeLife(homeApprove);
		addMessage(redirectAttributes, msg);
		return "redirect:"+Global.getAdminPath()+"/approve/activityApprove/?repage";
	}
	
}